<?php
// 本类由系统自动生成，仅供测试用途
namespace Admin\Controller;
use Think\Controller;
class PriceController extends Controller {
    public function index(){
        echo "lllll";die;
    }
    /**
	* 生成短信验证码并写入session	2016-11-21
	*/
	public function createsms(){
		$mobilephone = I('request.mobilephone');
		// $mobilephone = "17783641562";
		
		if(!check_mobilephone($mobilephone)){
			$this -> ajaxReturn(array('ret'=>0,'msg'=>'请输入正确的手机号'));
		}
		
		$checkcode = createverifycode();
		// $checkinfo = array(
		// 	'mobilephone' => $mobilephone,
		// 	'checkcode'   => $checkcode,
		// );
		//$content = '【筋斗车】您的验证码为'.$checkcode.'。如非本人操作，请忽略本短信';
		// messagelog($mobilephone,$content);
		$result = aliyunCodeVerify($mobilephone,$checkcode);
		$this -> ajaxReturn(array('ret'=>1,'checkinfo'=>$checkinfo));
	}
	/*获取品牌*/
	public function getAllVehicleOffer(){
		$cars   = M('qcgl_manage',null);
        $interestrate   = M('interest_manage',null);
        $brandDb=M('s_sjzd', null);
        $enquiry=M('enquiry_cars', null);
        $where = ['type' => '品牌','isdel'=>'0'];
        $brandData = $brandDb -> where($where) -> field('id, content as brand') -> select();
        $brands = [];
        foreach ($brandData as $k => $v) {
            if (!in_array(['brand' => $v['brand']], $brands)) {
                $brands[$v['id']] = ['brand' => $v['brand']];
            }
        }
        $data['brand']=$brands;
        $carData =  $enquiry->select();
        foreach($carData as $val){
            $data['cars'][$val['bid']][]=$val;
        }
        if($brandData){
        	$this->ajaxReturn(['success'=>"ok",'data'=>$data]);
        }else{
        	$this->ajaxReturn(['success'=>"error",'msg'=>'暂无品牌']);
        }
	}
	/*添加报价渠道*/
	public function getAddChannel(){
		$Channel   = I('post.');
		$adminId   = session('lampzuo_id');
		$enquiry   = M('enquiry_user',null);
		$usercar   = M('enquiry_user_car',null);
		$arr =[
			'name'=>$Channel['username'],
			'tel'=>$Channel['phone'],
			'store'=>$Channel['occupation'],
			'address'=>$Channel['address'],
			'local_address'=>$Channel['local_address'],
			'contact_tel'=>$Channel['urgenttel'],
			'contact'=>$Channel['urgentname'],
			'admin_id'=>$adminId,
			'create_time'=>time(),
			];
		$enquiryid = $enquiry->add($arr);
		if($enquiryid){
			if(is_array($Channel['multiselect'])){
				foreach ($Channel['multiselect'] as $key => $value) {
					$enparr = [
							'bid'=>$value,
							'uid'=>$enquiryid,
						];
					$usercar->add($enparr);
				}
				$this->ajaxReturn(['success'=>"ok",'msg'=>'创建成功']);
			}else{
				$this->ajaxReturn(['success'=>"error",'msg'=>'无品牌记录']);
			}
		}else{
			$this->ajaxReturn(['success'=>"error",'msg'=>'报价渠道创建失败']);
		}
	}
	/*报价渠道列表*/
	public function getListChannel(){
		$Channel   = I('post.');
		$adminId   = session('lampzuo_id');
		$enquiry   = M('enquiry_user',null);
		$usercar   = M('enquiry_user_car',null);
		$search = null;
		$keys = I('post.keys');
		if($keys){
			$search = "admin_id = ".$adminId." and enquiry_user.name like '%{$keys}%'";
		}else{
			$search = "admin_id = ".$adminId;
		}
		$count= $enquiry->where($search)->count();
		 // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page = new \Think\Page($count,6);
		$res = $enquiry->where($search)->limit($Page->firstRow.','.$Page->listRows)->select();
		$pageData=$Page->showAppAjax(); 
		// print_r($pageData);die;
		if($res){
			$this->ajaxReturn(['success'=>"ok",'msg'=>$res,'page'=>$pageData]);
		}else{
			$this->ajaxReturn(['success'=>"error",'msg'=>'暂无数据']);
		}
	}
	/*添加询价*/
	public function getAddSalePrice(){
		$Price   = I('post.');
		$adminId   = session('lampzuo_id');
		$enquiry   = M('enquiry_user',null);
		$usercar   = M('enquiry_user_car',null);
		$enquirycars   = M('enquiry_cars',null);
		$enquiryask   = M('enquiry_ask',null);
		$brandDb=M('s_sjzd', null);
		$time = time();
		$search = I('get.keys');
		if($Price['type'] == "on"){
			$arr = [
				  'guide_price'=>$Price['price'],
				  'bid'=>$Price['brandid'],
				  'name'=>$Price['usernameid'],
				];
			$Price['carid'] = $enquirycars->add($arr);
		}
		$carprice = $usercar->where(array('bid'=>$Price['brandid']))->select();
		$resadmid = $enquiryask->where(['carid'=>$Price['vehicleid'],'admin_id'=>$adminId])->order('version desc')->find();
		if(!$resadmid){
			$resadmid['version'] = 1;
		}else{
			$resadmid['version'] += 1;
		}
		foreach ($carprice as $key => $value) {
			$priceid = $Price['carid']?$Price['carid']:$Price['vehicleid'];
			$userres = $enquiry->where(array('id'=>$value['uid']))->find();
			$carsname= $enquirycars->where(array('id'=>$priceid))->find();
			$brand   = $brandDb->where(array('id'=>$Price['brandid']))->find();
			$token = base64_encode(uniqid());
			$arrcar = [
				  'desc'=>$Price['occupation'],
				  'carid'=>$priceid,
				  'admin_id'=>$adminId,
				  'uid'=>$value['uid'],
				  'create_time'=>$time,
				  'version'=>$resadmid['version'],
				  'token'=>$token,
				];
				if($userres['jl_type'] == $brand['jl_type)'] && $brand['id'] == "181"){
					$codelog = messagelog($userres['tel'],$carsname['name'].",".$carsname['guide_price'].",http://bj.uunewcar.com?token=".$token);//阿里云短信
					$enquiryaskID =$enquiryask->add($arrcar);
				}else if($brand['id'] != "181"){
					$codelog = messagelog($userres['tel'],$carsname['name'].",".$carsname['guide_price'].",http://bj.uunewcar.com?token=".$token);//阿里云短信
					$enquiryaskID =$enquiryask->add($arrcar);

				}
		}
		if($enquiryaskID){
			$this->ajaxReturn(['success'=>"ok",'msg'=>"询价成功",'retcode'=>$codelog]);
		}else{
			$this->ajaxReturn(['success'=>"error",'msg'=>'询价失败','retcode'=>$codelog]);
		}
	}
	/*询价*/
	public function getAddPriceList(){
		$enquiryask = M('enquiry_ask',null);
		$enquirycars= M('enquiry_cars',null);
		$adminId    = session('lampzuo_id');
		$brandDb    = M('s_sjzd', null);

		$enq 		= $enquiryask->where(['admin_id'=>$adminId])->select();
		$keys = I('post.keys');
		$search = null;
		if($keys){
			$search = "enquiry_cars.name like '%{$keys}%'";
		}
		$arrdata    = [];
		$carsarr    = [];
		$brandid    = "";
		$carsdata   = $enquirycars->where($search)->select();
		foreach ($carsdata as $key => $value) {
			$carsarr[$value['id']] = $value;
			$brandid .= $brandid == '' ? $value['bid'] : ','.$value['bid'];
		}
		$brandData = [];
		$brands = $brandDb -> where('id in ('.$brandid.')') -> select();
		foreach ($brands as $k => $v) {
			$brandData[$v['id']] = $v;
		}
		foreach ($enq as $key => $value) {
			$value['bid']         = $carsarr[$value['carid']]['bid'];
			$value['carname']     = $carsarr[$value['carid']]['name'];
			$value['create_time'] = date('Y-m-d H:i:s',$value['create_time']);
			$value['brand'] = \key_exists($value['bid'], $brandData) ? $brandData[$value['bid']]['content'] : '';
			if(!key_exists($value['admin_id'].$value['carid'].$value['version'],$arrdata)){
				if($value['carname']){
					$arrdata[$value['admin_id'].$value['carid'].$value['version']] = $value;
				}
			}
		}
		$i = 0;
		$arrcars = [];
		foreach ($arrdata as $key => $value) {
			$arrcars[$i] = $value;
			$i++;
		}
		$this->ajaxReturn(['data'=>$arrcars]);
	}

	/*报价列表*/
	public function getAddPriceOffer(){
		$enquiryask = M('enquiry_ask',null);
		$enquirycars= M('enquiry_cars',null);
		$adminId    = session('lampzuo_id');
		$carprice   = M('enquiry_car_price',null);
		$brandDb    = M('s_sjzd', null);
		// $enq 		= $enquiryask->where(['admin_id'=>$adminId])->select();
		// $cariddata  = implode(',', array_column($enq, 'carid'));
		$where		= '1=1 and type=2';
		$cardata    = $carprice->where($where)->select();
		$arrdata    = [];
		$carsarr    = [];
		$keys = I('post.keys');
		$search = null;
		if($keys){
			$search = "enquiry_cars.name like '%{$keys}%'";
		}
		$cartype=null;
		$brandid = '';
		$carsdata   = $enquirycars->where($search)->select();
		foreach ($carsdata as $key => $value) {
			$carsarr[$value['id']] = $value;
			$brandid .= $brandid == '' ? $value['bid'] : ','.$value['bid'];
		}
		$brandData = [];
		$brands = $brandDb -> where('id in ('.$brandid.')') -> select();
		foreach ($brands as $k => $v) {
			$brandData[$v['id']] = $v;
		}
		foreach ($cardata as $key => $value) {
			$value['bid'] = $carsarr[$value['carid']]['bid'];
			$cardata[$key]['carname'] = $carsarr[$value['carid']]['name'];
			$cardata[$key]['create_time'] =date('Y-m-d H:i:s',$value['create_time']);
			$cardata[$key]['brand'] = \key_exists($value['bid'], $brandData) ? $brandData[$value['bid']]['content'] : '';
			if(!$cardata[$key]['carname']){
				unset($cardata[$key]);
			}
		}
		$i = 0;
		$arrcars = [];
		foreach ($cardata as $key => $value) {
			$arrcars[$i] = $value;
			$i++;
		}
		$this->ajaxReturn(['data'=>$arrcars]);
	}

	/*报价列表*/
	public function getAddPriceData(){
		$enquiryuser= M('enquiry_user',null);
		$post = I('post.');
		$res = $enquiryuser->where(array('id'=>$post['id']))->find();
		if($res){
			$res['create_time'] = date('Y-m-d H:i:s',$res['create_time']);
			$this->ajaxReturn(['success'=>"ok",'data'=>$res]);

		}else{
			$this->ajaxReturn(['success'=>"error",'data'=>'暂无渠道,请查询后在试!']);
		}
	}
}